package com.aigou.admin.controller;

import com.aigou.admin.entity.PageEntity;
import com.aigou.admin.entity.ResponseEntity;
import com.aigou.admin.entity.Sales;
import com.aigou.admin.service.Salesservice;
import com.aigou.admin.service.impl.SalesserviceImpl;

import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;

@WebServlet("/sales")
public class SalesController extends BaseServlet{

    private Salesservice salesservice = new SalesserviceImpl();

    public void page(HttpServletRequest req, HttpServletResponse resp) throws IOException{
        String current = req.getParameter("current");
        String pageSize = req.getParameter("pageSize");
        PageEntity<Sales> salesPageEntity = salesservice.findSalesPage(Integer.valueOf(current),Integer.valueOf(pageSize));
        ResponseEntity.ok(resp,salesPageEntity);
    }


    public void findAll(HttpServletRequest req, HttpServletResponse resp) throws IOException {
        List<Sales> sales = salesservice.findAll();
        ResponseEntity.ok(resp,sales);
    }

    public void find(HttpServletRequest req, HttpServletResponse resp) throws IOException {
        String id = req.getParameter("id");
        Sales sales = salesservice.findByid(id);
        ResponseEntity.ok(resp,sales);
    }

    public void save(HttpServletRequest req, HttpServletResponse resp) throws IOException{
        String id = req.getParameter("id");
        String product_id = req.getParameter("product_id");
        String sales_num = req.getParameter("sales_num");
        Sales sales = new Sales(id,product_id,Integer.valueOf(sales_num));
        boolean rs = salesservice.save(sales);
        ResponseEntity.ok(resp,rs);
    }
    public void delete(HttpServletRequest req, HttpServletResponse resp) throws IOException{
        String id = req.getParameter("id");
        boolean rs = salesservice.delete(id);
        ResponseEntity.ok(resp,rs);
    }

    public void update(HttpServletRequest req, HttpServletResponse resp) throws IOException {
        String id = req.getParameter("id");
        String product_id = req.getParameter("product_id");
        String sales_num = req.getParameter("sales_num");
        Sales sales = new Sales(id,product_id,Integer.valueOf(sales_num));
        boolean rs = salesservice.update(sales);
        ResponseEntity.ok(resp,rs);
    }

}
